package pub.guoxin.oops.common.protocol;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.codec.DecoderException;
import org.springframework.beans.factory.annotation.Autowired;
import pub.guoxin.oops.common.protocol.constants.DataProtocol;
import pub.guoxin.oops.common.protocol.entity.DataProtocolHeader;
import pub.guoxin.oops.common.protocol.entity.DataProtocolPacket;
import pub.guoxin.oops.common.protocol.entity.map.DataProtocolCodeMap;
import pub.guoxin.oops.common.protocol.entity.map.DataProtocolCommandMap;
import pub.guoxin.oops.common.protocol.entity.map.DataProtocolTypeMap;
import pub.guoxin.oops.utils.BytesUtils;
import pub.guoxin.oops.utils.HexConvertUtils;


/**
 * Created by guoxin on 18-2-25.
 */
@Getter
@Setter
public class DataProtocolAnalysis {

    private DataProtocolCodeMap codeMap;

    private DataProtocolCommandMap commandMap;

    private DataProtocolTypeMap typeMap;

    private DataProtocolHeader analysisHeader(String data) throws DecoderException {
        DataProtocolHeader.DataProtocolHeaderBuilder builder = DataProtocolHeader.builder();
        {
            String hexString = BytesUtils.subStringByIndex(data, DataProtocol.BEGINNING_INDEX, DataProtocol.Header.FIXED);
            builder.fixed(hexString);
        }
        {
            String hexString = BytesUtils.subStringByIndex(data, DataProtocol.Header.FIXED, DataProtocol.Header.TOTAL_PACKET);
            int totalPacket = HexConvertUtils.hexString2Int(hexString);
            builder.totalPacket(totalPacket);
        }
        {
            String hexString = BytesUtils.subStringByIndex(data, DataProtocol.Header.TOTAL_PACKET, DataProtocol.Header.COMMAND);
            int commandCode = HexConvertUtils.hexString2Int(hexString);
            builder.command(codeMap);
        }
        return builder.build();
    }

    private void analysisPacket(String data) throws DecoderException {
        DataProtocolPacket.DataProtocolPacketBuilder builder = DataProtocolPacket.builder();
        {
            String hexString = BytesUtils.subStringByIndex(data, DataProtocol.BEGINNING_INDEX, DataProtocol.Packet.CODE);
            int code = HexConvertUtils.hexString2Int(hexString);
            builder.code(codeMap);
        }
        {
            String hexString = BytesUtils.subStringByIndex(data, DataProtocol.Packet.CODE, DataProtocol.Packet.TYPE);
            int type = HexConvertUtils.hexString2Int(hexString);
            builder.type(typeMap);
        }
        {

        }
    }

}
